package com.googlecode.maratische.google.gui;

import java.util.Enumeration;

import javax.swing.tree.DefaultMutableTreeNode;

import com.googlecode.maratische.google.model.Feed;

public class FeedNode extends DefaultMutableTreeNode {
	private Feed feed;
	
	public FeedNode(Feed feed) {
		this.feed = feed;
	}
	
	public FeedNode() {
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(this.getTitle());
		sb.append(" ").append(this.getUnreadItems());
		return sb.toString();
	}
	
	public Integer getId() {
		if (feed != null) {
			return feed.getId();
		} else {
			return 0;
		}
	}
	
	@SuppressWarnings("unchecked")
	public int getUnreadItems() {
		int result = 0;
		if (feed != null) {
			result += feed.getUnreadItems();
		}
		if (this.getChildCount()>0) {
			Enumeration<DefaultMutableTreeNode> childs = this.children();
			while (childs.hasMoreElements()) {
				FeedNode child = (FeedNode)childs.nextElement();
				if (child != null) {
					result += child.getUnreadItems();
				}
			}
		}
		return result;
	}

	public String getTitle() {
		if (feed != null) {
			return feed.getName();
		} else {
			return "rss";
		}
	}

	public String getXmlUrl() {
		if (feed != null) {
			return feed.getXmlUrl();
		} else {
			return "";
		}
	}
	public Feed getFeed() {
		return feed;
	}

	public void setFeed(Feed feed) {
		this.feed= feed;
	}
	
	public boolean isOfflineSupport() {
		if (this.feed != null && this.feed.getOfflineSupport() != null) {
			return this.feed.getOfflineSupport().booleanValue();
		}
		return false;
	}

	public void setOfflineSupport(boolean offlineSuport) {
		if (this.feed != null) {
			this.feed.setOfflineSupport(offlineSuport);
		}
	}
	
	public boolean isFolder() {
		if (this.feed != null && this.feed.isFolder()) {
			return true;
		}
		return false;
	}
	
	private static final long serialVersionUID = -4285810027448857769L;

}
